package com;

import com.fz.entity.Book;
import com.fz.mapper.BookMapper;
import com.fz.mybatis.MyBatisHelper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by webrx on 2017-08-24.
 */
public class Demo {
    @Test
    public void show(){
        //封装类代替连接数据库和加载器的步骤
        SqlSession s=new MyBatisHelper<BookMapper>().getSession();
        //实例化方法接口
        BookMapper bdao=s.getMapper(BookMapper.class);
        //数据插入
        Book book = new Book();
        book.setPrice(50);
        book.setName("《html设计》");
        try {
            book.setBdate(new SimpleDateFormat("yyyy-MM-dd").parse("2015-06-20"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        bdao.insert(book);
        //查询指定id
        Book bk=bdao.queryById(3);
        System.out.println(bk.getName());
        System.out.println("------------------");


        //数据查询
        List<Book> bks=bdao.query();
        for (Book b: bks){
            System.out.println(b.getName());
        }
        s.commit();
    }
}
